import '../css/table.css';
import '../css/newsClass.css';
import ajax from '../js/ajax';

const form = document.forms[0];
const list = document.getElementById('list');
let Total_data;
let Total_id;

drawTable();

form.onsubmit = function (e) {
    e.preventDefault();
    let url;
    if (Total_id) {
        url = '/api/admin/modifyNewsClass';
    } else {
        url = '/api/admin/addNewsClass';
    }
    ajax('post', url, {
        className: form.className.value,
        classExplain: form.classExplain.value,
        Total_id
    }).then(({ flag, msg }) => {
        if (flag) {
            alert(msg);
            drawTable();
        } else {
            alert(msg);
        }
    })
}

function drawTable() {
    list.innerHTML = '';
    ajax('get', '/api/news/getNewsClass')
        .then(({ flag, data }) => {
            if (flag) {
                Total_data = data;
                data.forEach(item => {
                    const tr = document.createElement('tr');
                    let str = '';
                    str += `<td>${item.id}</td>`;
                    str += `<td>${item.classname}</td>`;
                    str += `<td>${item.classexplain}</td>`;
                    str += `<td>${item.createdAt.formatDate('yyyy-MM-dd hh:mm:ss')}</td>`;
                    str += `<td>${item.updatedAt.formatDate('yyyy-MM-dd hh:mm:ss')}</td>`;
                    tr.innerHTML = str;

                    const td = document.createElement('td');

                    const a = document.createElement('a');
                    a.innerHTML = '修改';
                    a.dataset.id = item.id;
                    td.appendChild(a);
                    a.onclick = modifyClass;

                    const b = document.createElement('a');
                    b.innerHTML = '删除';
                    b.dataset.id = item.id;
                    td.appendChild(b);
                    b.onclick = deleteClass;

                    tr.appendChild(td);
                    list.appendChild(tr);
                });
            } else {
                alert('获取新闻分类失败');
            }
        })
}

function modifyClass() {
    Total_id = this.dataset.id;
    let obj;
    Total_data.forEach(item => {
        if (item.id == Total_id) {
            obj = item;
        }
    })
    form.className.value = obj.classname;
    form.classExplain.value = obj.classexplain;
    form.btn.innerHTML = '修改';
    form.cancel.style.display = 'inline';
}

function deleteClass() {
    if (!confirm('确定删除此新闻分类？')) {
        return;
    }
    ajax('post', '/api/admin/deleteNewsClass', { Total_id: this.dataset.id })
        .then(({ flag, msg }) => {
            if (flag) {
                alert(msg);
                drawTable();
            }
        })
}

form.cancel.onclick = back;

function back() {
    form.btn.innerHTML = '新增';
    form.cancel.style.display = 'none';
    Total_id = null;
    form.reset();
}

String.prototype.formatDate = function (arg) {
    const date = new Date(this);
    arg = arg.replace('yyyy', date.getFullYear());
    arg = arg.replace('MM', ('0' + (date.getMonth() + 1)).slice(-2));
    arg = arg.replace('dd', ('0' + date.getDate()).slice(-2));
    arg = arg.replace('hh', ('0' + date.getHours()).slice(-2));
    arg = arg.replace('mm', ('0' + date.getMinutes()).slice(-2));
    arg = arg.replace('ss', ('0' + date.getSeconds()).slice(-2));
    return arg;
}